<template>
  <div class="container">
    <div class="upload-file-box" v-loading="uploadLoading" @mouseenter="toolbarShow=true" @mouseleave="toolbarShow=false">
      <el-upload
        ref="uploadEl"
        :disabled="disabled"
        :action="uploadFilesUrl"
        :show-file-list="false"
        class="image-uploader"
        :name="option.name"
        :headers="headers"
        :on-success="handleSuccess"
        :before-upload="beforeUpload">
        <i class="iconfont df-iconweb-icon- image-uploader-icon"></i>
      </el-upload>
      <img v-if="imageUrl" :src="imageUrl" class="image-viewer" title="点击图片预览">
      <transition name="fade">
        <div v-if="imageUrl && toolbarShow && !disabled" class="image-toolbar">
          <i class="iconfont df-iconshanchu image-toolbar-del" title="删除" @click="handleDel"></i>
          <i class="iconfont df-iconluru image-toolbar-update" title="重新上传" @click="handleUpdate"></i>
        </div>
      </transition>
    </div>
    <div class="upload-title-box" :title="option.lable">
      <el-tooltip class="item" effect="dark" content="必填" placement="top">
        <i class="el-icon-star-on" v-if="option.required" style="color: #F56C6C;"></i>
      </el-tooltip>
      <label for="" :title="option.label">{{option.label}}</label>
    </div>
  </div>
</template>
<script>
export default {
  name: 'pbUpload',
  props: {
    option: Object,
    field: String,
    default: String,
    disabled: Boolean,
    uploadFilesUrl: String,
    headers: Object
  },
  data () {
    return {
      uploadLoading: false,
      imageUrl: '',
      toolbarShow: false
    }
  },
  watch: {
    default (val) {
      // todo 父组件传过来值
      this.imageUrl = val
    },
    uploadFilesUrl (val) {
      console.log(val)
    }
  },
  computed: {
    fileInputRefs () {
      // 上传组件中的 input
      return this.$refs['uploadEl'].$refs['upload-inner'].$refs['input']
    }
  },
  methods: {
    // eslint-disable-next-line
    handleSuccess(res, file) {
      // this.imageUrl = URL.createObjectURL(file.raw);
    },
    beforeUpload(file) {
      console.log(file)
      this.uploadLoading = true
      //todo 将在这里进行文件上传到服务器 此处为模拟
      let fileFormData = new FormData();
      fileFormData.append('file', file);
      
      fileFormData.append('attachmentClassCode', this.option.attachmentClassCode);
      fileFormData.append('attachmentType', this.option.attachmentType);
      fileFormData.append('attachmentTypeCode', this.option.attachmentTypeCode);
      let requestConfig = {
        headers: {
        'Content-Type': 'multipart/form-data'
        },
      }
      this.$http.post(this.uploadFilesUrl, fileFormData, requestConfig).then((res) => {
        this.uploadLoading = false
        if (res.status !== 200 || !res.data ) return this.$message.error('图片上传失败，请重新上传')
        if (res.data.code !== 10000) return this.$message.error(res.data.msg || '图片上传失败，请重新上传')
        let data = res.data.data
        let _param = {
          field: this.field,
          // url: data.annexId
          url: '/yw164api' + data.url
        }
        this.$emit('uploadChange', _param)
        this.imageUrl = '/yw164api' + data.url;
      }).catch(() => {

      })
      
      // window.setTimeout(() => {
      //   this.uploadLoading = false
      //   let _param = {
      //     field: this.field,
      //     url: URL.createObjectURL(file)
      //   }
      //   this.$emit('uploadChange', _param)
      //   this.imageUrl = URL.createObjectURL(file);
      // }, 500)
      return false
    },
    handleUpdate () {
      this.$refs.uploadEl.$refs['upload-inner'].$refs.input.click()
    },
    handleDel () {
      this.$confirm(`确定删除【${this.option.label}】附件？`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let _param = {
          field: this.field,
          url: ''
        }
        this.$emit('uploadChange', _param)
        this.imageUrl = ''
      }).catch(()=>{})
    }
  },
  created () {
      this.imageUrl = this.default
  }
}
</script>
<style scoped="scoped" lang="scss">
  .upload-container{
    height: 200px;
    .upload-file-box {
      position: relative;
      margin: 0 auto;
      width: 95%;
      height: 160px;
      background: rgba(240,240,240,1);
      .image-uploader {
        position: absolute;
        top: 0;
        left: 0;
        z-index: 1;
        width: 100%;
        .image-uploader-icon {
          font-size: 43px;
          color: #8c939d;
          width: 100%;
          height: 160px;
          line-height: 160px;
          text-align: center;
        }
      }
      .image-viewer {
        position: absolute;
        top: 0;
        left: 0;
        z-index: 2;
        width: 100%;
        height: 160px;
        display: block;
        cursor: pointer;
      }
      .image-toolbar {
        position: absolute;
        top: 0;
        left: 0;
        z-index: 3;
        width: 100%;
        height: 40px;
        line-height: 40px;
        background: rgba(0, 0, 0, .7);
        .iconfont {
          float: right;
          margin-right: 8px;
          padding: 0 4px;
          font-size: 20px;
          color: #ffffff;
          cursor: pointer;
        }
        .iconfont:hover {
          color: #C7000B;
        }
      }
    }
    .upload-title-box {
      margin: 0 auto;
      width: calc(95% - 2px);
      height: 39px;
      line-height: 39px;
      border:1px solid rgba(221,221,221,1);
      border-top: none;
      text-align: center;
      text-overflow:ellipsis; 
      overflow:hidden;
      white-space:nowrap;
    }
    .fade-enter-active, .fade-leave-active {
      transition: opacity .5s;
    }
    .fade-enter, .fade-leave-to /* .fade-leave-active below version 2.1.8 */ {
      opacity: 0;
    }
  }
</style>
